home *** CD-ROM | disk | FTP | other *** search
/ SunSoft Catalyst CDWARE 1996 May to August / Catalyst CDWARE 1996 May to August.iso / .products / .bin / httpd / Solaris_x86 / newwais.pl < prev    next >
Perl Script  |  1996-03-19  |  5KB  |  191 lines

  1. #!./perl
  2. #
  3. # newwais.pl -- WAIS search interface
  4. #
  5. # from wais.pl
  6. #
  7. # Tony Sanders <sanders@bsdi.com>, Nov 1993
  8. #
  9. # Example configuration (in local.conf):
  10. #     map topdir wais.pl &do_wais($top, $path, $query, "database", "title")
  11. #
  12. # this script uses a sneaky feature of Mosaic that interpretes a 
  13. # single text input form with the name 'isindex' (case sensitive) to
  14. # the same as a <ISINDEX>.  On non-mosaic clients, you wind up with an
  15. # additional query
  16. #
  17. # Note that I know even less about perl than the other two people
  18. # who hacked this, so feel free to send hate mail to pjh@netcom.com
  19. # if I did something really bad (or if there is a better way of
  20. # grabbing the filename off the end of a path.
  21. #
  22.  
  23. # parse disc name from ServerName Environt Variable with this script
  24.  
  25. do './variables.pl';
  26.  
  27. $oldLIB = $ENV{"LD_LIBRARY_PATH"};
  28. $ENV{"LD_LIBRARY_PATH"} = "$oldLIB:/usr/ucblib";
  29.  
  30. #require 'ctime.pl';
  31.  
  32. &get_request;
  33.  
  34. $waisq = "./waisq";
  35. $waisd = "/tmp/.wais/wais";
  36.  
  37.  
  38. $src = "catalyst_catalog";
  39. $title = "Example Data";
  40. #
  41. # file type map based on file extension, since all file types
  42. # come back type URL
  43. #
  44.  
  45. %filetype = (
  46. 'html', 'HTML File',
  47. );
  48.  
  49. #
  50. # code
  51.  
  52. sub send_index {
  53.     print "Content-type: text/html\n\n";
  54.     
  55.     print "<HEAD>\n<TITLE>Index of ", $title, "</TITLE>\n</HEAD>\n";
  56.     print "<BODY bgcolor=\"#DDDDDD\">\n<H1>", $title, "</H1>\n";
  57.  
  58.     print "This is an index of the information on this server. Please\n";
  59.     print "type a query in the search dialog.\n<P>";
  60.     print "You may use compound searches, such as: <CODE>environment AND cgi</CODE>\n";
  61.     print "<ISINDEX>";
  62. }
  63. sub get_request {
  64.  
  65.     if ($ENV{'REQUEST_METHOD'} eq "POST") {
  66.         read(STDIN, $request, $ENV{'CONTENT_LENGTH'});
  67.     } elsif ($ENV{'REQUEST_METHOD'} eq "GET" ) {
  68.         $request = $ENV{'QUERY_STRING'};
  69.     }
  70.  
  71.  
  72.     @names = &url_decode(split(/[&=]/, $request));
  73.     %rqpairs = @names;
  74.  
  75. }
  76.  
  77. sub url_decode {
  78.  
  79. #       Decode a URL encoded string or array of strings
  80. #               + -> space
  81. #               %xx -> character xx
  82.  
  83.  
  84.     foreach (@_) {
  85.         tr/+/ /;
  86.         s/%(..)/pack("c",hex($1))/ge;
  87.     }
  88.     @_;
  89. }
  90.  
  91.  
  92. sub do_wais {
  93. #    local($top, $path, $query, $src, $title) = @_;
  94.  
  95. # strip the escape off of *'s
  96.     for (@ARGV){s/\\//g};
  97.  
  98.     local(@query) = $rqpairs{"isindex"};;
  99.  
  100.     local($pquery) = join(" ", @query);
  101.     local($nquery) = $pquery;
  102.     $nquery =~ tr/[A-Z]/[a-z]/;
  103.     if (!(($nquery =~ / and /) || ($nquery =~ / or /)))
  104.     {
  105.        $nquery =~ s/ //g;
  106.     }
  107. #
  108. # grab a wais source if there is one
  109. #
  110.  
  111.     local($test) = $ENV{'PATH_INFO'};
  112.     if ($test)
  113.     {
  114.     $test =~ s/\///;
  115.         $src = "catalyst_catalog";
  116.         $title = $test;
  117.     }
  118.  
  119.     close STDERR;
  120.     open(STDERR, ">/dev/null");
  121.  
  122.     print "Content-type: text/html\n\n";
  123.  
  124.     $ENV{'HOME'} = "/";
  125.     
  126.     open(WAISQ, "-|") || exec ($waisq, "-c", $waisd, "-m", 100,
  127.                                 "-f", "-", "-S", "$src.src", "-g", $nquery);
  128.  
  129.     print "<HEAD>\n<TITLE>Search of ", "Catalyst Catalog $disc_rest", "</TITLE>\n</HEAD>\n";
  130.     print "<img src=\"/.wais/images/goto_home.gif\">\n";
  131.     print "<BODY bgcolor=\"#DDDDDD\">\n<H1>Catalyst Catalog on $disc_name $disc_rest</H1>\n";
  132.  
  133.     print "<HR><FORM method=\"POST\" action=\"/cgi-bin/newwais.pl/$src\">\n";
  134.     print "Enter keyword(s):\n";
  135. #    print "<input name=\"isindex\" value=\"@query\" size=30></FORM><HR>\n";
  136.     print "<input name=\"isindex\" value=\"$pquery\" size=30></FORM><HR>\n";
  137.  
  138.     print "$title contains the following\n";
  139.     print "items relevant to <B>\`$pquery\':</B><P>\n";
  140.     print "<center>";
  141.     print "<table border=5>";
  142.     print "<DL>\n";
  143.     print "<th> Document Matched</th>";
  144.     print "<tr>\n";
  145.  
  146.  
  147.     local($hits, $score, $headline, $lines, $bytes, $type, $date);
  148.  
  149.     print "<OL>";
  150.     while (<WAISQ>) {
  151.         /:score\s+(\d+)/ && ($score = $1);
  152.         /:number-of-lines\s+(\d+)/ && ($lines = $1);
  153.         /:number-of-bytes\s+(\d+)/ && ($bytes = $1);
  154.         /:type "(.*)"/ && ($type = $1);
  155.         /:headline "(.*)"/ && ($headline = $1);         # XXX
  156.         /:date "(\d+)"/ && ($date = $1, $hits++, &docdone);
  157.     }
  158.     print "</table>";
  159.     print "</center>";
  160.  
  161.     print "</OL>";
  162.     close(WAISQ);
  163.     print "</DL>\n";
  164.  
  165.     if ($hits == 0) {
  166.         print "Nothing found.\n";
  167.     }
  168.     print "</BODY>\n";
  169. }
  170.  
  171.  
  172. sub docdone {
  173.  
  174.     if ($headline =~ /Search produced no result/) {
  175.         print "<HR>";
  176.     print "<h1>Search produced no result.</h1>";
  177.     } else {
  178.         $docname = $headline;
  179.     $docname =~ s/\.([^.]*)$//;
  180.     $extension= $1;
  181.     $docname =~ s/\/([^\/]*)$//;
  182.     $docname = $1;
  183.         print "<td><LI><A HREF=\"$headline\"><B>$docname</B></A></td>\n";
  184.     print "<tr>";
  185.     }
  186.     $score = $headline = $lines = $bytes = $type = $date = '';
  187. }
  188.  
  189. eval '&do_wais';
  190.